package xyz.xpman.prisonerfitness.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import org.jetbrains.annotations.NotNull;

public class ScoringUtil {
    public static final String TAG = "[openMarketStore]";
    public static final String HMS_PACKAGE = "com.huawei.appmarket";

    private ScoringUtil() { }


    // 注：appId 是在华为控制台创建应用的id
    public static void openHmsStore(@NotNull Context context, final String appId) {
        showHmsStore(context, HMS_PACKAGE, appId);
    }

    public static void showHmsStore(@NotNull Context context,
                                    final String marketPkg,
                                    final String appId) {
        try {
            String appPkg = context.getPackageName();

            if (StrKit.isBlank(appPkg)) {
                return;
            }

            Uri uri = Uri.parse("market://details?id=" + appPkg);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);

            if (!StrKit.isBlank(marketPkg)) {
                intent.setPackage(marketPkg);
            }

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            if (intent.resolveActivity(context.getPackageManager()) == null) {
                uri = Uri.parse("https://appstore.huawei.com/app/C" + appId);
                Log.d(TAG, "huawei :" + uri);
                intent = new Intent(Intent.ACTION_VIEW, uri);
            }
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void openGooglePlayStore(@NotNull Context context) {
        showGooglePlayStore(context);
    }

    public static void showGooglePlayStore(@NotNull Context context) {
        String appPkg = context.getPackageName();
        String url = "http://play.google.com/store/apps/details?id=" + appPkg;
        Log.d(TAG, "GooglePlay :" + url);
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    }



}
